package com.jy.horny.utils

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.fragment.app.FragmentActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.lib.module_base.dialog.PermissionPopup
import com.lib.module_base.utils.photo.SelectPhotoUtil

/**
 * 打开相册和照相机的工具类
 */
object OpenAlbumCameraUtil {

    //打开相册
    fun openAlbum(context: FragmentActivity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_MEDIA_IMAGES
                ) != PackageManager.PERMISSION_DENIED && ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_MEDIA_VIDEO
                ) != PackageManager.PERMISSION_DENIED
            ) {
                SelectPhotoUtil.selectPhoto(context)
            } else {
                XXPermissions.with(context)
                    .permission(
                        listOf(
                            Manifest.permission.READ_MEDIA_IMAGES,
                            Manifest.permission.READ_MEDIA_VIDEO
                        )
                    )
                    .request(object : OnPermissionCallback {

                        override fun onGranted(
                            permissions: MutableList<String>,
                            allGranted: Boolean
                        ) {
                            if (!allGranted) {
                                //toast("获取部分权限成功，但部分权限未正常授予")
                                return
                            }
                            SelectPhotoUtil.selectPhoto(context)
                        }

                        override fun onDenied(
                            permissions: MutableList<String>,
                            doNotAskAgain: Boolean
                        ) {
                            if (doNotAskAgain) {
                                //toast("被永久拒绝授权，请手动授予权限")
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                //XXPermissions.startPermissionActivity(context, permissions)
                                PermissionPopup(context, 4).showPopupWindow()
                            } else {
                                //toast("获取权限失败")
                            }
                        }

                    })
            }
        } else {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) != PackageManager.PERMISSION_DENIED
            ) {
                SelectPhotoUtil.selectPhoto(context)
            } else {
                XXPermissions.with(context)
                    .permission(Permission.WRITE_EXTERNAL_STORAGE)
                    .request(object : OnPermissionCallback {

                        override fun onGranted(
                            permissions: MutableList<String>,
                            allGranted: Boolean
                        ) {
                            if (!allGranted) {
                                //toast("获取部分权限成功，但部分权限未正常授予")
                                return
                            }
                            SelectPhotoUtil.selectPhoto(context)
                        }

                        override fun onDenied(
                            permissions: MutableList<String>,
                            doNotAskAgain: Boolean
                        ) {
                            if (doNotAskAgain) {
                                //toast("被永久拒绝授权，请手动授予录音和日历权限")
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                //XXPermissions.startPermissionActivity(context, permissions)
                                PermissionPopup(context, 4).showPopupWindow()
                            } else {
                                //toast("获取录音和日历权限失败")
                            }
                        }

                    })
            }
        }
    }

    //打开相机
    fun openCamera(context: FragmentActivity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.CAMERA
                ) != PackageManager.PERMISSION_DENIED
            ) {
                SelectPhotoUtil.selectCamera(context)
            } else {
                XXPermissions.with(context)
                    .permission(Permission.CAMERA)
                    .request(object : OnPermissionCallback {
                        override fun onGranted(
                            permissions: MutableList<String>,
                            allGranted: Boolean
                        ) {
                            if (!allGranted) {
                                //toast("获取部分权限成功，但部分权限未正常授予")
                                return
                            }
                            SelectPhotoUtil.selectCamera(context)
                        }

                        override fun onDenied(
                            permissions: MutableList<String>,
                            doNotAskAgain: Boolean
                        ) {
                            if (doNotAskAgain) {
                                //toast("被永久拒绝授权，请手动授予权限")
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                //XXPermissions.startPermissionActivity(context, permissions)
                                PermissionPopup(context, 1).showPopupWindow()
                            } else {
                                //toast("获取权限失败")
                            }
                        }
                    })
            }
        } else {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) != PackageManager.PERMISSION_DENIED && ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.CAMERA
                ) != PackageManager.PERMISSION_DENIED
            ) {
                SelectPhotoUtil.selectCamera(context)
            } else {
                XXPermissions.with(context)
                    .permission(listOf(Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE))
                    .request(object : OnPermissionCallback {
                        override fun onGranted(
                            permissions: MutableList<String>,
                            allGranted: Boolean
                        ) {
                            if (!allGranted) {
                                //toast("获取部分权限成功，但部分权限未正常授予")
                                return
                            }
                            SelectPhotoUtil.selectCamera(context)
                        }

                        override fun onDenied(
                            permissions: MutableList<String>,
                            doNotAskAgain: Boolean
                        ) {
                            if (doNotAskAgain) {
                                //toast("被永久拒绝授权，请手动授予权限")
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                //XXPermissions.startPermissionActivity(context, permissions)
                                PermissionPopup(context, 1).showPopupWindow()
                            } else {
                                //toast("获取权限失败")
                            }
                        }
                    })
            }
        }
    }
}